package com.itheima.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    //添加资源的处理器
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 以后只要客户端拿着 /backend/... 的路径过来访问，就马上到类路径里面的 backend里面去找内容出来展示。
        registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
        registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");
    }

    //重写消息转换器方法,把我们的消息转换放置到集合中去
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //1.创建消息转换器
        MappingJackson2HttpMessageConverter mc = new MappingJackson2HttpMessageConverter();

        //2.添加转换规则到转换器
        mc.setObjectMapper(new JacksonObjectMapper());

        //3.将消息转换器放到参数集合
        converters.add(0,mc);
    }
}
